package com.lindroy.androidplayer.logic.network.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.lindroy.androidplayer.logic.model.*
import com.lindroy.androidplayer.logic.network.base.repository.BaseRepository
import com.lindroy.androidplayer.logic.network.datasource.NetworkDataSource
import com.lindroy.androidplayer.logic.network.datasource.pagingsource.*
import com.lindroy.androidplayer.utils.StateLiveData
import kotlinx.coroutines.flow.Flow
import kotlin.coroutines.CoroutineContext

/**
 * @author Lin
 * @date 2022/1/7
 * @function 唯一的数据源
 */
object Repository : BaseRepository() {

    fun login(pwd: String, name: String) = fire { NetworkDataSource.login(pwd, name) }

    fun register(userName: String, password: String, rePassword: String) =
        fire { NetworkDataSource.register(userName, password, rePassword) }

    fun logout() = fire { NetworkDataSource.logout() }

    fun getBanner() = fire { NetworkDataSource.getBanner() }

    val homeArticle: Flow<PagingData<ArticleResp>>
        get() = createPager { HomeArticlePagingSource() }

    fun getProjectTypes() = StateLiveData { NetworkDataSource.getProjectTypes() }

    fun getProjectList(cid: Int) = createPager { ProjectListPagingSource(cid) }

    fun getWxChapters() = StateLiveData { NetworkDataSource.getWxArticleChapters() }

    fun getWxArticles(id: Int) = createPager { WxArticlePagingSource(id) }

    val knowledgeSystemList: Flow<PagingData<KnowledgeResp>>
        get() = createPager(Int.MAX_VALUE, Int.MAX_VALUE) { KnowledgeSystemPagingSource() }

    fun getKnowledgeArticleList(cid: Int): Flow<PagingData<ArticleResp>> =
        createPager { KnowledgeArticlePagingSource(cid) }

    val squareArticles: Flow<PagingData<ArticleResp>>
        get() = createPager { SquareArticlePagingSource() }

    val questionList: Flow<PagingData<ArticleResp>>
        get() = createPager { QuestionListPagingSource() }

    fun getNavigationData() = StateLiveData { NetworkDataSource.getNavigationData() }

    fun getMyCoinInfo() = fire { NetworkDataSource.getMyCoinInfo() }

    val coinHistory: Flow<PagingData<CoinHistoryResp>>
        get() = createPager { CoinHistoryPagingSource() }

    val coinRankingList: Flow<PagingData<CoinRankingResp>>
        get() = createPager { CoinRankingPagingSource() }

    val collectedArticles: Flow<PagingData<CollectedArticleResp>>
        get() = createPager { CollectedArticlesPagingSource() }

    fun getCollectedWebsitesResp() = StateLiveData { NetworkDataSource.getCollectedWebsite() }

    val myShareArticles: Flow<PagingData<ArticleResp>>
        get() = createPager { MyShareArticlesPagingSource() }

    val searchHotkeyList = StateLiveData { NetworkDataSource.getSearchHotKey() }

    fun getSearchResult(keyword: String) = createPager { SearchResultPagingSource(keyword) }

    private fun <T : Any> createPager(
        pageSize: Int = 20,
        initialLoadSize: Int = 20,
        block: () -> BasePagingSource<*, T>
    ) = Pager(
        //initialLoadSize 参数不传的话，第一次加载时会默认加载 PAGE_SIZE 三倍的数据
        config = PagingConfig(pageSize, initialLoadSize = initialLoadSize),
        pagingSourceFactory = { block() }).flow

}